package cn.dida.service.impl;

import java.util.ArrayList;
import java.util.List;

import cn.dida.dao.LgsCustDao;
import cn.dida.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.dida.dao.LgsBillsDao;
import cn.dida.model.LgsBillsProduct;
import cn.dida.service.LgsBillService;

/**
 * <p>物流单据 接口实现</p>
 * @auther lisha
 * @date 2017-2-9 14:02:30
 */
@Service
public class LgsBillServiceImpl implements LgsBillService {
	
	@Autowired
	private LgsBillsDao lgsBillsDao;

	@Autowired
	private LgsCustDao lgsCustDao;

	@Override
	public Boolean insertLgsBills(LgsBills lgsBills) {
		return lgsBillsDao.insertLgsBills(lgsBills) == 1;
	}

	@Override
	public Pageable<LgsBills> query(Integer companyId, String productName,
			String billsNo, Integer billsType, Integer node, Integer nubsS,
			Integer nubsE,Integer scanNubsS, Integer scanNubsE, Integer from, Integer to,Integer depotScan, String cTimeS,
			String cTimeE, Integer sta, int page) {
		Pageable<LgsBills> pageList = lgsBillsDao.query(companyId, productName, billsNo, billsType, node, nubsS, nubsE,scanNubsS, scanNubsE, from, to,depotScan, cTimeS, cTimeE, sta, page);
		List<LgsBills> billsList = pageList.getResult();
		List<LgsBills> list = new ArrayList<>();
		for(LgsBills bill:billsList){

			List<LgsBillsProduct> BillsPro = lgsBillsDao.getProductByBillId(bill.getId());
			bill.setBillsProductList(BillsPro);
			list.add(bill);
		}
		pageList.setResult(list);
		return pageList;
	}

	@Override
	public List<LgsBills> queryForExport (Integer companyId, String productName, String billsNo, Integer billsType, Integer node,
										  Integer scanNubsS, Integer scanNubsE,Integer nubsS, Integer nubsE,
										  Integer from, Integer to,Integer depotScan, String cTimeS, String cTimeE, Integer sta){
		List<LgsBills> billsList = lgsBillsDao.queryForExport(companyId, productName, billsNo, billsType, node,scanNubsS, scanNubsE, nubsS, nubsE, from, to,depotScan, cTimeS, cTimeE, sta);
		List<LgsBills> list = new ArrayList<>();
		for(LgsBills bill:billsList){
			List<LgsBillsProduct> BillsPro = lgsBillsDao.getProductByBillId(bill.getId());
			bill.setBillsProductList(BillsPro);
			list.add(bill);
		}
		return list;
	}

	@Override
	public LgsBills getLgsBillById(int id, int companyId) {

		return lgsBillsDao.getLgsBillById(id, companyId);
	}

	@Override
	public List<LgsBillsProduct> getProductByBillId(int billId) {
		return lgsBillsDao.getProductByBillId(billId);
	}

	@Override
	public boolean isExistsBillNo(Integer companyId, String billNo,Integer type) {
		return lgsBillsDao.isExistsBillNo(companyId, billNo,type);
	}

	@Override
	public boolean billInvalid(Integer companyId, int id) {
		return lgsBillsDao.billInvalid(companyId, id);
	}


	@Override
	public String getComanyErpCode(Integer companyId){
		return lgsBillsDao.getComanyErpCode(companyId);
	}

	/**
	 * 获取地点列表
	 * @param companyId 公司id
	 * @param sta 是否可用，1可用，0屏蔽
	 * @return List<LgsDepot> 地点列表
	 * @date 2017-02-13 15:17
	 */
	@Override
	public List<LgsDepot> getDepotList(Integer companyId, Integer sta) {
		return lgsBillsDao.getDepotList(companyId, sta);
	}

	/**
	 * 获取同一公司下状态正常的 客户对象（收发货对象用）
	 * @param companyId
	 * @param state
	 * @return
	 */
	@Override
	public List<LgsCust> getCustList(Integer companyId, Integer state){
		return lgsCustDao.getList(companyId,state);
	}

	/**
	 * 根据输入收/发对象名称或编码  获取列表
	 * @param keyword
	 * @param limit
	 * @param companyId
	 * @return
	 */
	@Override
	public List<LgsCust> getCustSearchList(String keyword, Integer limit, Integer companyId) {
		return lgsCustDao.getSearchList(keyword,limit,companyId);
	}

	@Override
	public LgsBillsProduct getTotalNubsByBillId(int billId) {
		return lgsBillsDao.getTotalNubsByBillId(billId);
	}

	@Override
	public boolean updateBills(LgsBills lgsBills) {
		return lgsBillsDao.updateBills(lgsBills) == 1;
	}

	@Override
	public Pageable<LgsLogPdaScan> queryBillsDetail(String billId, Integer companyId,
			String batchNub, String qrcodeBoxId, String qrcodeBottleId,
			int page) {
		return lgsBillsDao.queryScanDetail(billId, companyId, batchNub, qrcodeBoxId, qrcodeBottleId, page);
	}

}
